package com.bafenyi.zh.bafenyilib.util;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.fragment.app.FragmentActivity;

import com.bafenyi.zh.bafenyilib.config.BFYConfig;
import com.blankj.utilcode.util.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * Created by T on 2018/6/28
 * Preferenced工具类
 */

public class PreferenceUtil {

    private static String preferences_name = "preferences";

    public static void put(String key, String value) {
        putString(key, value);
    }

    public static void put(String key, int value) {
        putInt(key, value);
    }

    public static void put(String key, boolean value) {
        putBoolean(key, value);
    }

    public static void put(String key, float value) {
        putFloat(key, value);
    }

    public static void put(String key, long value) {
        putLong(key, value);
    }

    public static void put(String key, Set<String> values) {
        putStringSet(key, values);
    }

    private static void putString(String key, String value) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    private static void putInt(String key, int value) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    private static void putBoolean(String key, boolean value) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    private static void putFloat(String key, float value) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public static void putLong(String key, long value) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    private static void putStringSet(String key, Set<String> values) {
        SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putStringSet(key, values);
        editor.apply();
    }

    public static long getLong(String key, long defValue) {
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getLong(key, defValue);
    }

    public static int getInt(String key, int defValue) {
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getInt(key, defValue);
    }

    public static float getFloat(String key, float defValue) {
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getFloat(key, defValue);
    }

    public static String getString(String key, String defValue) {
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getString(key, defValue);
    }

    public static boolean getBoolean(String key, boolean defValue){
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getBoolean(key,defValue);
    }

    public static Set<String> getStringSet(String key, Set<String> defValue){
        return BFYConfig.getApp().getSharedPreferences(preferences_name , 0)
                .getStringSet(key,defValue);
    }

    public static void remove(String... keys) {
        if (keys != null) {
            SharedPreferences sharedPreferences = BFYConfig.getApp().getSharedPreferences(preferences_name , 0);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            for (String key : keys) {
                editor.remove(key);
            }
            editor.apply();
        }
    }


    /**
     * 获取两个日期之间的间隔天数
     */
    public static int getGapCount(String startStr, String endStr) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        long startMillis = TimeUtils.string2Millis(startStr, sdf);
        long endMillis = TimeUtils.string2Millis(endStr, sdf);

        return (int) ((endMillis - startMillis) / (1000 * 60 * 60 * 24));
    }


    public static long getTimeExpend(long startTime) {

        return (System.currentTimeMillis() - startTime) / (24 * 60 * 60 * 1000);
    }




    private static String FirstSetNotificationDate = "FirstSetNotificationDate";
    /**
     *
     */
    public static void setFirstSetNotificationDate(long n) {
        PreferenceUtil.putLong(FirstSetNotificationDate, n);
    }

    /**
     *
     */
    public static long getFirstSetNotificationDate() {
        return PreferenceUtil.getLong(FirstSetNotificationDate, 0);
    }
    public static void toSetting(FragmentActivity activity) {
        ApplicationInfo appInfo = activity.getApplicationInfo();
        String pkg = activity.getApplicationContext().getPackageName();
        int uid = appInfo.uid;
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                //这种方案适用于 API 26, 即8.0（含8.0）以上可以用
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkg);
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid);
                //这种方案适用于 API21——25，即 5.0——7.1 之间的版本可以使用
                intent.putExtra("app_package", pkg);
                intent.putExtra("app_uid", uid);
                activity.startActivityForResult(intent, 0);
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                intent.setData(Uri.parse("package:" + BFYConfig.getPkgName()));
                activity.startActivityForResult(intent, 0);
            } else {
                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                activity.startActivityForResult(intent,0);
            }
        } catch (Exception e) {
            Intent intent = new Intent(Settings.ACTION_SETTINGS);
            activity.startActivityForResult(intent, 0);
        }
    }
}
